package com.stlm2.dems.transfer.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.Convert;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.transfer.entity.Transfer;
import com.stlm2.dems.transfer.dao.TransferDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service("transferService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class TransferService extends BaseServiceImpl<Transfer> {

	@Resource
	private TransferDao transferDao;
	@Resource
	private UserService userService;
	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <Transfer> getDao() {
		return transferDao;
	}

	public List<ModelMap> convert(List<Transfer> records) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if(records != null && records.size() > 0){
			for(Transfer transfer:records){
				ModelMap map = buildModel(transfer);
				list.add(map);
			}
		}
		return list;
	}

	public ModelMap buildModel(Transfer transfer) {
		ModelMap model = new ModelMap();
		User user=userService.getById(transfer.getUserId());
		User toUser=userService.getById(transfer.getToUserId());
		if(transfer!=null){
			model.put("code",transfer.getCode());
			model.put("creatTime", Convert.toYMDHMS(transfer.getCreatTime()));
			model.put("userNmae",user.getRealName()!=null?user.getRealName():"***");
			model.put("userPhone",user.getName());
			model.put("toUserName",toUser.getRealName()!=null?toUser.getRealName():"***");
			model.put("toUserPhone",toUser.getName());
			model.put("money",transfer.getMoney().toString());
			model.put("state",transfer.getOrderState().toString());

		}
		return model;
	}

	/*
	*zlq
	* 查询所有用户的转账记录
	* @param  token
	* @param  page:页码
	* @param  userName:转款账户
	* @param  stateValue:状态值
	* @param  toUserName:收款账户
	* @param  code：编码
	*
	 */
	public String getAllTransferRecord(ModelMap map ,String page, String token ,String  userName ,String stateValue,String toUserName,String code){
		Transfer transfer =new Transfer();
		PageView pageview =new  PageView();
		int pageNo = 1;
		int pageSize=20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (org.springframework.util.StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", map);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", map);
		}
		if(NumberUtils.isNumber(userName)){
			User user=userDao.getByName(userName.trim());
			if(user!=null){
				transfer.setUserId(user.getId());
			}
		}
		if(NumberUtils.isNumber(toUserName)){
			User user=userDao.getByName(toUserName.trim());
			if(user!=null){
				transfer.setToUserId(user.getId());
			}
		}
		if(!StringUtils.isEmpty(code)){
			transfer.setCode(code.trim());
		}
		if(NumberUtils.isNumber(stateValue)){
			transfer.setOrderStateValue(Integer.parseInt(stateValue));
		}

		pageview=transferDao.queryPageView(pageNo, pageSize,transfer);
		if (pageview != null){
			map.put("datas",convertList(pageview.getRecords()));
			map.put("recordsTotal", pageview.getRowCount());
			map.put("totalPage", pageview.getPageCount());
			map.put("pageNow", pageview.getPageNow());
			map.put("code",1);
			map.put("msg","success");
		}
		return  new JSONObject(map).toString();
	}
	public List<ModelMap> convertList(List<Transfer> transferList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (transferList != null && transferList.size() > 0) {
			for (Transfer transfer : transferList) {
				ModelMap map = buildModelList(transfer);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(Transfer transfer) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		ModelMap model = new ModelMap();
		User user=userDao.getById(transfer.getUserId());
		User toUser=userDao.getById(transfer.getToUserId());
		model.put("id",transfer.getId());
		model.put("userAccount",user.getName());
		model.put("code",transfer.getCode());
		model.put("toUserAccount",toUser.getName());
		model.put("money",transfer.getMoney());
		model.put("creatTime",formatter.format(transfer.getCreatTime()));
		model.put("orderState",transfer.getOrderState().toString());

		return  model;
	}
}